package com.ty.xiaoyu.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class FileUtil {

    private static final String seperator = System.getProperty("file.separator");

    private static final DateTimeFormatter DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH-mm-ss'T'");

    private static final Random r = new Random();

    private static String getImgBasePath() {
        String os = System.getProperty("os.name");
        String basePath = "";
        if (os.toLowerCase().startsWith("win")) {
            basePath = "E:/xiaoyu_temp";
        } else {
            basePath = "/home/xiaoyu/xiaoyu_temp";
        }
        basePath = basePath.replace("/", seperator);
        return basePath;
    }

    /**
     * 将地址分隔符替换成当前系统路径分隔符
     * @param path
     * @return
     */
    public static String replaceSeperatorPath(String path) {
        return path.replace("/", seperator);
    }

    /**
     * 获取帖子图片存储地址
     * @return 帖子图片存储地址
     */
    public static String getTopicImagePath() {
        String imgBasePath = getImgBasePath();
        String topicImagePath = imgBasePath + "/topic/";
        topicImagePath = topicImagePath.replace("/", seperator);
        return topicImagePath;
    }

    /**
     * 获取帖子用户头像存储地址
     * @return 帖子用户头像存储地址
     */
    public static String getUserImagePath() {
        String imgBasePath = getImgBasePath();
        String topicImagePath = imgBasePath + "/user/";
        topicImagePath = topicImagePath.replace("/", seperator);
        return topicImagePath;
    }

    /**
     * 生成随机文件名：当前时分秒+五位随机数
     * @return 文件名（不带后缀）
     */
    public static String getRandomFileName() {
        // 取10000到99999之间的随机数
        int randomNum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000;
        String nowTime = DATE_TIME_FORMATTER.format(LocalDateTime.now());
        return nowTime + randomNum;
    }

    /**
     * 保存文件
     * @param path 保存地址（包括文件名）
     * @param data 文件的字节流
     * @throws IOException
     */
    public static void saveFile(String path, byte[] data) throws IOException {
        File file = new File(path);
        if (file.exists()) {
            throw new RuntimeException("已存在同名文件");
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(data);
        } catch (IOException e) {
            throw e;
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
    }

    /**
     * 删除文件
     * @param path
     * @return
     */
    public static boolean deleteFile(String path) {
        path.replace("/", seperator);
        File file = new File(path);
        if (file.exists()) {
            if (file.isFile()) {
                return file.delete();
            }
        }
        return false;
    }


}
